package actions;

import java.sql.*;
import java.util.*; 
import support.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.StudentModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;

import forms.StudentsForm;

public class VerificationAction extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String confirm = (String)request.getParameter("confirm");
		if (confirm.equals("false"))
			return mapping.findForward("cancelsuccess");
		
		Vector degrees = (Vector)request.getSession().getAttribute("degreeList");
		
		StudentsForm iForm = (StudentsForm) form;
		
		int appID = -1;
		
		try {
			appID = StudentModel.writeApplicant(iForm.getFirstName(), iForm.getLastName(), iForm.getMiName(), iForm.getCitizenship(), 
				iForm.getResidency(), iForm.getAddress(), iForm.getCity(), iForm.getState(), iForm.getZipCode(), 
				iForm.getAreaCode(), iForm.getCountryCode(), iForm.getPhone(), iForm.getPermanent(), iForm.getSpecialization(),
				request.getUserPrincipal().getName());
		} catch (DbException ex) {
			ex.printStackTrace();
		}
		
		request.getSession().setAttribute("appID", appID);
		
		for (int i = 0; i < degrees.size(); i++) {
			try {
				StudentModel.writeDegree((HashMap)degrees.get(i), appID);
			} catch (DbException ex) {
				ex.printStackTrace();
			}
		}
			
		return mapping.findForward("submitsuccess");
	}
}